<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home/wellcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>日志管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增日志</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card" style="margin-top:20px;">
      <div slot="header" class="clearfix">
        <span>
          <i class="el-icon-menu"></i> 操作面板
        </span>
      </div>
      <!-- 表单部分 -->
      <el-row>
        <el-col :span="8">
          <!--
             属性解释：
              model：存放表单数据的对象
              rules：表单校验规则
          ref：对表单进行重置操作-->
          <el-form label-width="80px" :model="ruleForm" :rules="rules" ref="ruleForm">
            <!-- 商家图片 -->
            <el-form-item label="日志图片" prop="d_img">
              <!-- 
                文件上传组件 
                action:上传文件服务器地址
              -->
              <el-upload
                class="avatar-uploader"
                action="http://localhost:9001/upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="ruleForm.d_img" :src="ruleForm.d_img" class="avatar" fit="cover" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <!-- 表单其他信息 -->
            <el-form-item label="伴宠人员" prop="d_man_name">
              <el-input type="text" v-model="ruleForm.d_man_name" placeholder="请输入伴宠人员姓名" />
            </el-form-item>
            <el-form-item label="发布城市" prop="d_city">
              <el-input type="text" v-model="ruleForm.d_city" placeholder="请输入日志发布城市" />
            </el-form-item>
            <el-form-item label="发布时间" prop="d_time">
              <el-date-picker
                value-format="yyyy-MM-dd hh:mm:ss"
                v-model="ruleForm.d_time"
                type="datetime"
                placeholder="请选择日志发布时间"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="内容详情" prop="d_detail">
              <el-input
                type="textarea"
                v-model="ruleForm.d_detail"
                :rows="2"
                placeholder="请输入日志内容"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-check" @click="submitForm('ruleForm')">添加</el-button>
              <el-button
                type="warning"
                icon="el-icon-refresh-right"
                @click="resetForm('ruleForm')"
              >重置</el-button>
              <el-button type="info" icon="el-icon-back" @click="back">返回</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表单校验规则
      rules: {
        d_img: [
          { required: true, message: "请选择日志照片", trigger: "change" }
        ],
        d_man_name: [
          { required: true, message: "请填写伴宠人员姓名", trigger: "blur" }
        ],
        d_city: [
          { required: true, message: "请填写日志发布城市", trigger: "blur" }
        ],
        d_time: [
          { required: true, message: "请选择日志发布日期", trigger: "blur" }
        ],
        d_detail: [
          { required: true, message: "请填写日志内容", trigger: "blur" }
        ]
      },
      // 表单数据
      ruleForm: {
        // 接收表单数据
        d_img: "", // 图片上传成功后，位于服务器的地址
        d_time: "",
        d_city: "",
        d_detail: ""
      }
    };
  },
  methods: {
    // 返回
    back() {
      this.$router.back();
    },
    // 提交表单
    submitForm(formName) {
      // 校验表单，提示：校验之前，需要在rules中配置校验规则
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$http
            .post(this.$api.addDairy, this.ruleForm)
            .then(res => {
              if (res?.data?.code == 200) {
                this.$message.success(res?.data?.msg);
                // 清空表单
                this.resetForm("ruleForm");
              } else {
                this.$message.error(res?.data?.msg);
              }
            })
            .catch(err => {
              this.$message.error(err);
            });
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleAvatarSuccess(res, file) {
      //this.ruleForm.d_img = URL.createObjectURL(file.raw);
      this.ruleForm.d_img = file?.response?.data;
      console.log("图片位于服务器的地址：", this.ruleForm.d_img, file);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG或者PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    }
  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>